﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;

namespace luanvanFinal
{
    /// <summary>
    /// Interaction logic for wdProgressForm.xaml
    /// </summary>
    public partial class wdProgressForm : Window
    {
        int idPro;
        public wdProgressForm()
        {
            InitializeComponent();
        }

        public void loadProject()
        {
            String strSQL = "select proName from project";
            clsConnect ketnoi = new clsConnect();
            ketnoi.openConnection();
            SqlDataAdapter da = new SqlDataAdapter(strSQL, ketnoi.Connection);
            DataTable dt = new DataTable();
            da.Fill(dt);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cbProject.Items.Add(dt.Rows[i][0]);
            }
            cbProject.SelectedIndex = 0;
            ketnoi.closeConnection();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            loadProject();

        }

        private void btnShow_Click(object sender, RoutedEventArgs e)
        {
            
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            int idProject;
            clsProject project = new clsProject();
            idProject = Convert.ToInt32(project.getIdFromName(cbProject.Text));
            wdProgressEdit progressEdit = new wdProgressEdit();
            progressEdit.IdProject = idProject;
            progressEdit.Show();
        }
        public void loadForm()
        {
            int STT = 1;
            int idProject;
            clsProject project = new clsProject();
            idProject = Convert.ToInt32(project.getIdFromName(cbProject.Text));
            idPro = Convert.ToInt32(project.getIdFromName(cbProject.Text));
            clsStage stage = new clsStage();
            String SQL = "select title,startDay, endDay, description,totalPercent from stage where idProject='" + idProject + "'";
            DataTable dt = stage.getDataStage(SQL);
            DataTable getDt = new DataTable();
            getDt.Columns.Add("Num");
            getDt.Columns.Add("Stage Title");
            getDt.Columns.Add("Start Day");
            getDt.Columns.Add("End Day");
            getDt.Columns.Add("Description");
            getDt.Columns.Add("Total Percent");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = getDt.NewRow();
                dr[0] = STT.ToString();
                dr[1] = dt.Rows[i][0].ToString();
                dr[2] = dt.Rows[i][1].ToString();
                dr[3] = dt.Rows[i][2].ToString();
                dr[4] = dt.Rows[i][3].ToString();
                dr[5] = dt.Rows[i][4].ToString();
                STT++;
                getDt.Rows.Add(dr);
                getDt.AcceptChanges();
            }
            dgStage.ItemsSource = getDt.DefaultView;
        }
        private void dtgRequiment_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {

        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            DataRowView row = (DataRowView)dgStage.SelectedItems[0];
            String stageTitle = row[1].ToString();
            String SQLIdStage = "select Id from stage where idProject='"+idPro+"' and title = '"+stageTitle+"'";
            clsStage stage = new clsStage();
            DataTable dt = stage.getDataStage(SQLIdStage);
            wdProgress progress = new wdProgress();
            progress.IdStage = Convert.ToInt32(dt.Rows[0][0].ToString());
            
            progress.Show();
        }

        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            DataRowView row = (DataRowView)dgStage.SelectedItems[0];
            String stageTitle = row[1].ToString();
            wdStageDetailShow stageDetailShow = new wdStageDetailShow();
            stageDetailShow.IdProject = idPro;
            stageDetailShow.Title = stageTitle;
            stageDetailShow.Show();
        }

        private void MenuItem_Click_2(object sender, RoutedEventArgs e)
        {
            DataRowView row = (DataRowView)dgStage.SelectedItems[0];
            String stageTitle = row[1].ToString();
            String SQL = "select Id from stage where idProject= '"+idPro+"' and title='"+stageTitle+"'";
            clsStage stage = new clsStage();
            DataTable dt = stage.getDataStage(SQL);
            wdInsertStageDetail insertDetail = new wdInsertStageDetail();
            insertDetail.IdStage = Convert.ToInt32(dt.Rows[0][0].ToString());
            insertDetail.Show();
        }

        private void MenuItem_Click_3(object sender, RoutedEventArgs e)
        {
            DataRowView row = (DataRowView)dgStage.SelectedItems[0];
            String stageTitle = row[1].ToString();
            wdStageEdit stageEdit = new wdStageEdit();
            stageEdit.IdProject = idPro;
            stageEdit.StageTitle = stageTitle;            
            stageEdit.Show();

        }

        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {
            loadForm();
        }

        private void MenuItem_Click_4(object sender, RoutedEventArgs e)
        {
            try
            {
                DataRowView row = (DataRowView)dgStage.SelectedItems[0];
                String stageTitle = row[1].ToString();
                String SQL = "select Id from stage where idProject= '" + idPro + "' and title='" + stageTitle + "'";
                clsStage stage = new clsStage();
                DataTable dt = stage.getDataStage(SQL);
                stage.IdProject = idPro;
                stage.Title = stageTitle;

                if (dt != null)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        clsStageDetail stageDetail = new clsStageDetail();
                        stageDetail.IdStage = Convert.ToInt32(dt.Rows[i][0].ToString());
                        stageDetail.deleteStageDetailByIdStage();
                    }
                }
                stage.deleteStage();
                loadForm();
            }
            catch
            {

            }
        }
    }
}
